package cn.bud.msgbus.consumer.client;

import com.zl9c.general.bus.client.provider.ProviderProxy;
import com.zl9c.general.bus.test.service.TestProvider;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

import javax.annotation.Resource;
import java.util.List;

//@Service
public class ProviderRegister implements ImportBeanDefinitionRegistrar {

    private ConfigurableListableBeanFactory beanFactory;

    @Resource
    private BusProviderScanner providerScanner;


    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry,
                                        BeanNameGenerator importBeanNameGenerator) {
        AbstractBeanDefinition abd = BeanDefinitionBuilder.genericBeanDefinition(TestProvider.class).
                getBeanDefinition();

    }

    //初始化时将自定义的bean放入spring容器中使用
    public void afterPropertiesSet() throws Exception {
        List<ProviderProxy> providerProxies = providerScanner.scanProviders("com.zl9c.**");
        for (ProviderProxy providerProxy : providerProxies) {
            beanFactory.registerSingleton(providerProxy.getBusBeanName(), providerProxy.getProxyObj());
        }
    }
}
